package at.codebase.web.servlets.item;

import java.util.ArrayList;

import at.codebase.core.log.Log;
import at.codebase.core.search.ItemSearch;
import at.codebase.db.dao.EImageDao;
import at.codebase.db.dao.EItemDao;
import at.codebase.db.dao.EItemFeedbackDao;
import at.codebase.db.entities.EImage;
import at.codebase.db.entities.EItem;
import at.codebase.db.entities.EItemFeedback;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.URLParameters.ParameterException;
import at.codebase.web.util.servlet.SecureServlet;

public class ItemAction extends SecureServlet{

	@Override
	protected RequestType requestType() {
		return RequestType.GET;
	}
	
	@Override
	protected void initSecurityLevels(ArrayList<UserLevel> secLevels) {
		secLevels.add(UserLevel.ADMIN);
		secLevels.add(UserLevel.REGISTERED);
	}

	@Override
	protected void processRequestSecurityLevel(RequestInformation requestInfo, UserLevel level, RequestType requestType) {
		if(!tokenCorrect(requestInfo)) {
			processRequestNoAccess(requestInfo, level);
			return;
		}
		
		try {
			String actionType = requestInfo.getParameters().getString("type");
			//Deactivate Item
			if(actionType.equals("deactivate")){
				long itemId = requestInfo.getParameters().getLong("itemId");
				EItem item = EItemDao.getItemById(new Long(itemId));
				if(item != null){
					if(item.isActive()){
						if(item.checkSeller(requestInfo.getUser().getId())){
							item.setActive(false);
							ItemSearch.removeItem(item);
							EItemDao.saveItem(item);
							Log.logItem(requestInfo.getUser().getId(),
									"User(seller) (userid="
											+ requestInfo.getUser().getId()
											+ ") deactivated an item (itemid="
											+ itemId + ")");
						}else if(level.equals(UserLevel.ADMIN)){
							item.setActive(false);
							ItemSearch.removeItem(item);
							EItemDao.saveItem(item);
							Log.logAdmin(requestInfo.getUser().getId(),
									"Admin (userid="
											+ requestInfo.getUser().getId()
											+ ") deactivated an item (itemid="
											+ itemId + ")");
						}else{
							//TODO Logging - Unauthorized Change
						}
					}
				}
				redirectTo("/item/"+itemId);
			//Activate Item
			}else if(actionType.equals("activate")){
				long itemId = requestInfo.getParameters().getLong("itemId");
				EItem item = EItemDao.getItemById(new Long(itemId));
				if(item != null){
					if(!item.isActive()){
						if(item.checkSeller(requestInfo.getUser().getId())){
							item.setActive(true);
							ItemSearch.putItem(item);
							EItemDao.saveItem(item);
							Log.logItem(requestInfo.getUser().getId(),
									"User(seller) (userid="
											+ requestInfo.getUser().getId()
											+ ") activated an item (itemid="
											+ itemId + ")");
						}else if(level.equals(UserLevel.ADMIN)){
							item.setActive(true);
							ItemSearch.putItem(item);
							EItemDao.saveItem(item);
							Log.logAdmin(requestInfo.getUser().getId(),
									"Admin (userid="
											+ requestInfo.getUser().getId()
											+ ") activated an item (itemid="
											+ itemId + ")");
						}else{
							//TODO Logging - Unauthorized Request
						}
					}
				}
				redirectTo("/item/"+itemId);
			//Delete Item
			}else if(actionType.equals("delete")){
				long itemId = requestInfo.getParameters().getLong("itemId");
				EItem item = EItemDao.getItemById(new Long(itemId));
				if(item != null){
					if(item.checkSeller(requestInfo.getUser().getId())){
						EItemDao.removeItem(item);
						Log.logItem(requestInfo.getUser().getId(),
								"User(seller) (userid="
										+ requestInfo.getUser().getId()
										+ ") removed an item (itemid="
										+ itemId + ")");
					}else if(level.equals(UserLevel.ADMIN)){
						EItemDao.removeItem(item);
						Log.logAdmin(requestInfo.getUser().getId(),
								"Admin (userid="
										+ requestInfo.getUser().getId()
										+ ") removed an item (itemid="
										+ itemId + ")");
					}else{
						//TODO Logging - Unauthorized Request
					}
				}
				redirectTo("/user/profile");
			//Remove Feedback
			}else if(actionType.equals("removeFeedback")){
				//Is not really the itemId (safes URL rewrite work)
				long feedbackId = requestInfo.getParameters().getLong("itemId");
				EItemFeedback feedback = EItemFeedbackDao.getFeedbackById(new Long(feedbackId));
				long itemId = feedback.getItem().getId();
				if(feedback != null){
					if(level.equals(UserLevel.ADMIN)){
						EItemFeedbackDao.removeItemFeedback(feedback);
						Log.logAdmin(requestInfo.getUser().getId(),
								"Admin (userid="
										+ requestInfo.getUser().getId()
										+ ") removed a item feedback (itemid="
										+ itemId + ")");
					}else{
						//TODO Logging - Unauthorized Request
					}
					redirectTo("/item/"+itemId+"#feedback");
				}
			//Remove Image
			}else if(actionType.equals("removeImage")){
				//Is not really the itemId (safes URL rewrite work)
				long imageId = requestInfo.getParameters().getLong("itemId");
				EImage image = EImageDao.getImageById(new Long(imageId));
				
				if(image != null){
					EItem item = EItemDao.getItemById(new Long(image.getItem().getId()));
					if(image.getUploader().getId() == requestInfo.getUser().getId().longValue()){
						if(item != null){
							item.getImageKeys().remove(EImage.key(image.getId()));
							EItemDao.saveItem(item);
						}
						EImageDao.removeImage(image);
						Log.logItem(requestInfo.getUser().getId(),
								"User(uploader) (userid="
										+ requestInfo.getUser().getId()
										+ ") removed an image (imageid="+image.getId()+") from an item (itemid="
										+ (item != null?item.getId():"") + ")");
					}else if(item != null && item.checkSeller(requestInfo.getUser().getId())){
						item.getImageKeys().remove(EImage.key(image.getId()));
						EItemDao.saveItem(item);
						EImageDao.removeImage(image);
						Log.logItem(requestInfo.getUser().getId(),
								"User(seller) (userid="
										+ requestInfo.getUser().getId()
										+ ") removed an image (imageid="+image.getId()+") from an item (itemid="
										+ (item != null?item.getId():"") + ")");
					}else if(level.equals(UserLevel.ADMIN)){
						if(item != null){
							item.getImageKeys().remove(EImage.key(image.getId()));
							EItemDao.saveItem(item);
						}
						EImageDao.removeImage(image);
						Log.logAdmin(requestInfo.getUser().getId(),
								"Admin (userid="
										+ requestInfo.getUser().getId()
										+ ") removed an image (imageid="+image.getId()+") from an item (itemid="
										+ (item != null?item.getId():"") + ")");
					}else{
						//TODO Logging - Unauthorized Request
					}
					redirectTo("/item/upload/"+((item!=null)?item.getId()+"":""));
				}
			}
			
		} catch (ParameterException e) {
			//TODO: Wrong parameter.. Maybe Log this
		}
		
	}

}
